﻿using QWManage.API.GroupChatDetail92122;
using QWManage.DAL;
using QWManage.Logs;
using QWManage.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace QWManage.TimerTask
{
    public class GroupsTask
    {
        public static int Run(int minutes)
        {
            int max = 0;
            try
            {
                // 另外可以设置某些群。是可以间隔多久就采集一次。
                // 因为有一些群。目前在用的。比较活跃。
                // 所以得例如10分钟采集一次

                GroupDAL groupDAL = new GroupDAL();
                List<GroupInfo> groups = groupDAL.GetGroupInfos();
                CustomersGroupsDAL customersGroupsDAL = new CustomersGroupsDAL();
                Dictionary<int, List<GroupInfo>> dic = new Dictionary<int, List<GroupInfo>>();
                foreach (GroupInfo groupInfo in groups)
                {
                    if (groupInfo.Interval.HasValue && groupInfo.Interval > 0)
                    {
                        int interval = groupInfo.Interval.Value;
                        if (interval > max) max = interval;
                        if (!dic.ContainsKey(interval))
                            dic.Add(interval, new List<GroupInfo>());
                        dic[interval].Add(groupInfo);
                    }
                }

                foreach (int interval in dic.Keys)
                {
                    if (minutes % interval != 0) continue;
                    LogMgr.Log("定群采集", $"定时时间到 间隔{interval}分钟 采集 {dic[interval].Count} 个群");
                    LogMgr.Log("定群采集", "开时采集");
                    Stopwatch stopwatch = Stopwatch.StartNew();
                    //开始采集群
                    foreach (GroupInfo groupInfo in dic[interval])
                    {
                        string groupName = "";
                        GroupChatDetailArgs agrs = new GroupChatDetailArgs() { chat_id = groupInfo.ChatID };

                        string tokenA = API.AccessToken.TokenA.GetAccessToken();
                        List<CustomerGroupInfo> customerGroupInfos =
                            GroupChatDetailAPI.InvokePlus(tokenA, agrs, ref groupName);

                        //添加组，无则添加，有则更新
                        groupDAL.Append(groupInfo.ChatID, groupInfo.Name);

                        DateTime lastUpdate = DateTime.Now;
                        foreach (CustomerGroupInfo customerGroupInfo in customerGroupInfos)
                        {
                            customerGroupInfo.Status = true;
                            customerGroupInfo.LastUpdate = lastUpdate;
                            //无则添加，有则更新
                            customersGroupsDAL.Add(customerGroupInfo);
                        }
                        customersGroupsDAL.UpdateInGroupStatus(lastUpdate);
                    }
                    int total_seconds = (int)Math.Round(stopwatch.Elapsed.TotalSeconds);
                    int total_minutes = (int)Math.Round(stopwatch.Elapsed.TotalMinutes);
                    LogMgr.Log("定群采集", $"采集结束，用时 {total_seconds} 秒，约 {total_minutes} 分钟");
                }
            }
            catch (Exception ex)
            {
                LogMgr.Log(ex);
                LogMgr.Log("定群采集", "异常报错:" + ex.Message, LogLevel.Error);
            }
            return max;
        }
    }
}
